package MyProject1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9090);
        while (true) {
            ExecutorService executorService = Executors.newCachedThreadPool();
            executorService.submit(() -> {
                try (
                        Socket socket = serverSocket.accept();
                        InputStream is = socket.getInputStream();
                        OutputStream os = socket.getOutputStream();
                        //装换成高级流
                        DataInputStream dis = new DataInputStream(is);
                        DataOutputStream dos = new DataOutputStream(os);
                ) {

                    //先接收到来自客户端的数据
                    String accept = dis.readUTF();
                    //将该内容进行拆分为登录名、密码
                    String[] strings = accept.split("&");
                    String name = strings[0];
                    String password = strings[1];
                    String operate = strings[2];

                    //登录操作
                    if (operate.equals("login")) {
                        //接着查看文件中是否有该登录名且相对应的密码
                        //利用到了properties 文件来保存会读取
                        Properties properties = new Properties();
                        properties.load(new FileReader("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_6\\src\\MyProject1\\LoginNameAndPassword.properties"));

                        //获取全部键
                        Set<String> set = properties.stringPropertyNames();
                        for (String s : set) {
                            if (s.equals(name)) {
                                if (properties.getProperty(name).equals(password)) {
                                    //发送信息回到客户端
                                    dos.writeUTF("true");
                                }
                            }
                        }
                        dos.writeUTF("false");
                    }

                    //注册登录
                    if (operate.equals("enroll")) {
                        //先查询文件中是否有重名
                        Properties properties = new Properties();
                        properties.load(new FileReader("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_6\\src\\MyProject1\\LoginNameAndPassword.properties"));
                        Set<String> set = properties.stringPropertyNames();
                        for (String s : set) {
                            if (s.equals(name)) {
                                System.out.println("已经有重名啦！！！！");
                                dos.writeUTF("false");
                                break;
                            }
                        }
                        //如果查询没有重名，那么就可以将器登录名还有密码保存起来
                        properties.setProperty(name, password);
                        properties.store(new FileWriter("D:\\software\\code\\2023_java\\2023_java_code\\code_24_5_6\\src\\MyProject1\\LoginNameAndPassword.properties"), "成员注释");

                        dos.writeUTF("true");
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }

    }
}
